package com.lanou.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.bean.*;
import com.lanou.mapper.CpnUseRecordMapper;
import com.lanou.service.BankInfoService;
import com.lanou.service.CpnUseRecordService;
import com.lanou.service.VipInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 账户管理
 */
@RequestMapping("acc")
@Controller
public class AccountController {

    @Autowired
    private VipInfoService vipInfoService;

    @Autowired
    private BankInfoService bankInfoService;

    @Autowired
    private CpnUseRecordService cpnUseRecordService;

    /**
     * @Author shw   
     * @Description
     * @Param
     * @Date   展示会员信息,并进行分页
     */
    @RequestMapping("/accountPage")
    @ResponseBody
    public ModelAndView accountPage(VipInfo vipInfo,Integer currentPage) {
        System.out.println("前端获取的账户信息------"+vipInfo);
        ModelAndView modelAndView = new ModelAndView();
        //根据传过来的的银行卡账号查询vip_no
            String vipNo = vipInfoService.selectVipNoByBankCardNo(vipInfo.getBankCardNo());
            if(vipNo!=null){
                vipInfo.setVipNo(vipNo);
            }
        System.out.println("传入银行卡号后传过去的会员信息-------"+vipInfo);

        List<VipInfo> vipInfoList1 = vipInfoService.selectAllVipInfo1(vipInfo);
        for (VipInfo viplist:vipInfoList1
             ) {
            BankInfo bankInfo = vipInfoService.selectBankInfoByVipNo(viplist.getVipNo());
            viplist.setBankCardNo(bankInfo.getBankCardNo());
            viplist.setBankName(bankInfo.getBankName());
            viplist.setBankType(bankInfo.getBankType());
        }

        modelAndView.addObject("viplist",vipInfoList1);
        System.out.println("加入银行卡信息之后------"+vipInfoList1);

        //分页的信息
        System.out.println(currentPage);
        if (currentPage == null || currentPage < 1) {
            currentPage = 1;
        }
        PageHelper.startPage(currentPage, 1);


        List<VipInfo> vipInfoList = vipInfoService.selectAllVipInfo1(vipInfo);

        PageInfo<VipInfo> pageInfo = new PageInfo<VipInfo>(vipInfoList);
        pageInfo.getPageNum();
        pageInfo.getTotal();
        pageInfo.getPages();
        pageInfo.getNavigatepageNums();
        modelAndView.addObject("page", pageInfo);

        modelAndView.setViewName("/vip/accountPage");
        modelAndView.addObject("list", vipInfoList);
        return modelAndView;
    }

/**
 * @Author shw   
 * @Description
 * @Param
 * @Date   会员信息回显 
 */
    @RequestMapping("/modifyUpdate.do")
     public ModelAndView modifyUpdate(VipInfo vipInfo){
        ModelAndView modelAndView=new ModelAndView("vip/accountUpdate");
        modelAndView.addObject("result",vipInfo);
        return modelAndView;
    }

/**
 * @Author shw   
 * @Description
 * @Param
 * @Date   接收会员更新的信息,进行更新操作 
 */
    @RequestMapping("/updateBalance")
    @ResponseBody
    public String updateBalance(VipInfo vipInfo,String bankCardNo_b){
        System.out.println("----------会员信息更新"+vipInfo);
        System.out.println("接收到的银行卡信息-----"+bankCardNo_b);

        int vresult = vipInfoService.updateByVipNoSelective(vipInfo);
        System.out.println("更新会员信息-----"+vresult);

        //根据银行卡号获取id更新银行卡信息
        int id = bankInfoService.selectIdByBankCardNo(bankCardNo_b);
        BankInfo bankInfo=new BankInfo();
        bankInfo.setId(id);
        bankInfo.setVipNo(vipInfo.getVipNo());
        bankInfo.setBankCardNo(vipInfo.getBankCardNo());
        System.out.println("更新的银行卡信息------"+bankInfo);
        int bresult = vipInfoService.updateByPrimaryKeySelective(bankInfo);
        System.out.println("返回结果---"+vresult+bresult);
        if (vresult!=0&&bresult!=0){
            System.out.println("返回结果---"+"success");
            return "success";
        }else {
            System.out.println("返回结果---"+"false");
            return "false";
        }

    }

    /**
     * @Author shw   
     * @Description
     * @Param
     * @Date   根据会员编号查询积分使用记录 
     */
    @RequestMapping("/integralFind.do")
    public ModelAndView integralFind(String vipNo){
        ModelAndView modelAndView=new ModelAndView("vip/integralFind");


        //根据会员编号查询积分使用记录
        CpnRecord cpnRecord = vipInfoService.selectCpnRecordByVipNo(vipNo);
        System.out.println("获取积分使用记录-----"+cpnRecord);
        modelAndView.addObject("lm",cpnRecord);
        return modelAndView;

    }

/**
 * @Author shw   
 * @Description
 * @Param
 * @Date   查询代金券信息,进行展示
 */
    @RequestMapping("/findCoupon.do")
    public ModelAndView findCoupon(String vipNo){
        System.out.println("获取会员账号----"+vipNo);
        ModelAndView modelAndView=new ModelAndView("vip/findCoupon");

        //根据会员账号查询代金券编号,获取list集合
        List<CpnUseRecord> cpnUseRecordList = cpnUseRecordService.selectAllCpnUseRecordByVipNo(vipNo);
        System.out.println("获取到的代金券使用记录信息-----"+cpnUseRecordList);


        //根据代金券编号查询代金券信息
        //创建一个代金券信息的集合
        List<CpnInfo> cpnInfos=new ArrayList<>();
        for (CpnUseRecord cpn:cpnUseRecordList
             ) {
            CpnInfo cpnInfo = vipInfoService.selectCpnInfoByCpnNo(cpn.getCpnNo());
            if(cpnInfo!=null){
                cpnInfos.add(cpnInfo);
            }

        }
        System.out.println("获取会员的代金券信息------"+cpnInfos);
        modelAndView.addObject("list",cpnInfos);
        modelAndView.addObject("vipNo",vipNo);
         return modelAndView;
    }

}
